/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seagate.util.mail;

import com.seagate.app.report.IncommingBinExcel;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.security.Security;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;

import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.Region;

public class MailUtil {

    private String mailhost = "mh.tep.mailhost.seagate.com";

    public synchronized void sendMail(String subject, String body, String sender, String recipients, HSSFWorkbook wb)
            throws Exception {

        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.host", mailhost);
//        props.put("mail.smtp.auth", "true");
//        props.put("mail.smtp.port", "465");
//        props.put("mail.smtp.socketFactory.port", "465");
//        props.put("mail.smtp.socketFactory.class",
//                "javax.net.ssl.SSLSocketFactory");
//        props.put("mail.smtp.socketFactory.fallback", "false");
        //      props.setProperty("mail.smtp.quitwait", "false");



        Session session = Session.getDefaultInstance(props, null);
//                new javax.mail.Authenticator() {
//
//                    protected PasswordAuthentication getPasswordAuthentication() {
//                        return new PasswordAuthentication("tanit9999@gmail.com", "lovelove");
//                    }
//                });

        MimeMessage message = new MimeMessage(session);
        message.setSender(new InternetAddress(sender));
        message.setSubject(subject, "UTF-8");
//        message.setContent(body, "text/html; charset=UTF-8");
        //message.setSentDate(new Date());

        if (recipients.indexOf(',') > 0) {
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
        } else {
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
        }

        //
        // Set the email message text.
        //
        MimeBodyPart messagePart = new MimeBodyPart();
        messagePart.setContent(body, "text/html; charset=UTF-8");

        //
        // Set the email attachment file
        //

        String DATE_FORMAT = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        Calendar c1 = Calendar.getInstance(); // today

        String filename = "BIN_REPORT_" + sdf.format(c1.getTime()) + ".xls";
        // Create a workbook
        IncommingBinExcel excl = new IncommingBinExcel();









        FileOutputStream out = new FileOutputStream(filename);
        wb.write(out);


        MimeBodyPart attachmentPart = new MimeBodyPart();
        FileDataSource fileDataSource = new FileDataSource(filename) {

            public String getContentType() {
                return "application/octet-stream";
            }
        };
        attachmentPart.setDataHandler(new DataHandler(fileDataSource));
        attachmentPart.setFileName(filename);

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messagePart);
        multipart.addBodyPart(attachmentPart);

        message.setContent(multipart);



        Transport.send(message);

    }

    public static void main(String args[]) throws Exception {
        MailUtil mailutils = new MailUtil();
        //  mailutils.sendMail("test", "test", "tanit.sittayakorn@seagate.com", "tanit.sittayakorn@seagate.com");

    }
}
